//
//  UIView+Frame.m
//  SQBaseKit
//
//  Created by Seven on 2017/12/19.
//  Copyright © 2017年 Seven. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)

- (CGFloat)x{
    return self.left;
}

- (void)setX:(CGFloat)x{
    self.left = x;
}

- (CGFloat)y{
    return self.top;
}

- (void)setY:(CGFloat)y{
    self.top = y;
}


- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = cornerRadius;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}


- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = [borderColor CGColor];
}

- (void)setBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth{
    self.layer.borderColor = [borderColor CGColor];
    self.layer.borderWidth = borderWidth;
}

- (void)setShapeLayer:(CAShapeLayer *)shapeLayer{
    objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(@"shapeLayer"), shapeLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - shapLayer
- (void)setMaskCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
    
    CAShapeLayer *layer = self.shapeLayer;
    layer.frame = self.bounds;
    layer.borderColor = UIColor.clearColor.CGColor;
    layer.fillColor = UIColor.whiteColor.CGColor;
    layer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius].CGPath;
    self.layer.mask = layer;
    //
}

- (CAShapeLayer *)shapeLayer{
    CAShapeLayer *tmp = objc_getAssociatedObject(self, (__bridge const void *)(@"shapeLayer"));
    if (tmp == nil) {
        tmp = CAShapeLayer.layer;
        tmp.frame = self.bounds;
        //tmp.borderColor = UIColor.clearColor.CGColor;
        //tmp.fillColor = UIColor.whiteColor.CGColor;
        self.shapeLayer = tmp;
        self.layer.mask = tmp;
    }
    return tmp;
}


// Methods
-(void)centerToParent{
    if(self.superview){
        switch ([UIApplication sharedApplication].statusBarOrientation){
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:{
                self.origin     = CGPointMake((self.superview.height / 2.0) - (self.width / 2.0),
                                              (self.superview.width / 2.0) - (self.height / 2.0));
                break;
            }
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:{
                self.origin     = CGPointMake((self.superview.width / 2.0) - (self.width / 2.0),
                                              (self.superview.height / 2.0) - (self.height / 2.0));
                break;
            }
            case UIInterfaceOrientationUnknown:
                return;
        }
    }
}

@end
